//
//  PostColumnViewController.m
//  礼物go
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PostColumnViewController.h"


#import "SelectDetailViewController.h"



@interface PostColumnViewController ()<UITableViewDelegate,UITableViewDataSource>



@end

@implementation PostColumnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.titleName;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:   UIBarButtonSystemItemReply target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"<返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
//self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"order"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:  UIBarButtonSystemItemAction target:self action:@selector(rightButtonAction)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    
    
    self.dataArray = [NSMutableArray array];
    
    self.postCTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, self.view.frame.size.height - 66) style:UITableViewStylePlain];
    [self.view addSubview:self.postCTableView];
    self.postCTableView.dataSource = self;
    self.postCTableView.delegate = self;
    [self.postCTableView registerClass:[SelectTableViewCell class] forCellReuseIdentifier:postCCell];
    self.postCTableView.showsVerticalScrollIndicator = NO;
    
    self.nextPage = 0;
    [self getSelectList:self.nextPage];
    [self addHeader];
    [self addFooter];
    
    
    
    
    // Do any additional setup after loading the view.
}
#pragma mark --下拉刷新
-(void)addHeader{
    
    __block PostColumnViewController *postVC = self;
    // 添加上拉刷新尾部控件
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        postVC.nextPage = 0;//记录页码
        postVC.isUpLoading = NO;//标记为下拉操作
        [postVC getSelectList:postVC.nextPage];//重新请求数据
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
    //[header beginRefreshing];
    
    self.postCTableView.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i < 6; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"box_0%d",i]];
        [imageArray addObject:image];
    }
    [header setImages:imageArray forState:MJRefreshStateRefreshing];
    
    UIImage *pullImage = [imageArray firstObject];
    NSArray *pullArray = [NSArray arrayWithObjects:pullImage, nil];
    [header setImages:pullArray forState:MJRefreshStatePulling];
    
}


#pragma mark --上拉加载更多
-(void)addFooter{
    __block PostColumnViewController *postCVC = self;
    // 添加上拉刷新尾部控件
    self.postCTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        postCVC.nextPage ++;
        [postCVC getSelectList:postCVC.nextPage];//请求数据
    }];
}

-(void)getSelectList:(NSUInteger)nextCursor{
AFNetworkReachabilityManager *netWorkManger = [AFNetworkReachabilityManager sharedManager];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=%ld",self.dataId,20 * nextCursor];
[LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
    [netWorkManger stopMonitoring];
    
    if (self.isUpLoading == NO) {
        //说明是下拉，就要清空数组中的数据
        [self.dataArray removeAllObjects];
    }
    NSDictionary *dataDic = [result objectForKey:@"data"];
    NSArray *itemArray = [dataDic objectForKey:@"items"];
    for (NSDictionary *dic in itemArray) {
        SelectContentModel *contentModel = [[SelectContentModel alloc] init];
        [contentModel setValuesForKeysWithDictionary:dic];
        [self.dataArray addObject:contentModel];
    }
    [self.postCTableView.mj_header endRefreshing];
    [self.postCTableView.mj_footer endRefreshing];
    
    [self.postCTableView reloadData];

    
} error:^(NSError *error) {
    
    
}];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:postCCell];
    SelectContentModel *select = [self.dataArray objectAtIndex:indexPath.row];
    cell.selectContent = select;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectContentModel *selectModel = [self.dataArray objectAtIndex:indexPath.row];
    SelectDetailViewController *SDVC = [[SelectDetailViewController alloc] init];
    SDVC.dataId = selectModel.myId;
     SDVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:SDVC animated:YES];
    
}


-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
